package cn.yunyichina.reservation.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Lullaby on 2016/8/31.
 */
public class DateUtil {

    public static final String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYYMMDDHH = "yyyy-MM-dd HH";
    public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    public static String getIntervalDateString(int interval) {
        DateFormat formatter = new SimpleDateFormat(YYYYMMDD);
        Calendar calendar = Calendar.getInstance();
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH) - interval);
        return formatter.format(calendar.getTime());
    }

    public static String getDateString(String pattern) {
        DateFormat formatter = new SimpleDateFormat(YYYYMMDDHHMMSS);
        if (pattern == null) {
            return formatter.format(Calendar.getInstance().getTime());
        } else {
            return new SimpleDateFormat(pattern).format(Calendar.getInstance().getTime());
        }
    }

}
